package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EcMaterielElementsRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.EcMaterielElements;
import com.ndp.fb.rdb.model.EcMaterielElementsExt;

import java.util.List;

/**
 * Created by infi.he on 2016/4/28.
 */
@Service
public class EcMaterielElementsRdbServiceImpl extends DaoTemplate implements EcMaterielElementsRdbService {
    @Override
    public Long save(EcMaterielElements ecTemplateElements) {
        return saveResultId(ecTemplateElements);
    }

    @Override
    public List<EcMaterielElements> findByMaterielId(Long MaterielId) {
        String sql = "SELECT * from ec_materiel_elements ec where ec.template_materiel_id = ? and is_delete = 0";
        return getJdbcTemplate().query(sql, new Object[]{MaterielId}, DefaultRowMapper.autoRowMapper(EcMaterielElements.class));
    }

    @Override
    public List<EcMaterielElementsExt> findExtByMaterielId(Long MaterielId) {
        String sql = "SELECT\n" +
                "	*\n" +
                "FROM\n" +
                "	ec_materiel_elements ec,\n" +
                "  ec_element_category eec\n" +
                "WHERE\n" +
                "  ec.parent_code = eec.`code`\n" +
                "and ec.is_delete = 0 \n" +
                "and ec.template_materiel_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{MaterielId}, DefaultRowMapper.autoRowMapper(EcMaterielElementsExt.class));
    }

    @Override
    public void deleteElements(Long id) {
        String sql = "update ec_materiel_elements set is_delete = 1 where id = ?";
        getJdbcTemplate().update(sql,new Object[]{id});
    }

    @Override
    public EcMaterielElements findByMaterielIdAndElementIdAndParentCode(Long materielId, Long elementId, String parentCode) {
        String sql = "select * from ec_materiel_elements m  where m.template_materiel_id = ? and m.template_element_id = ? and m.parent_code = ?";
        return findOne(sql, new Object[]{materielId, elementId, parentCode}, DefaultRowMapper.autoRowMapper(EcMaterielElements.class));
    }
}
